package com.inwk.valoepi.managers;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.inwk.valoepi.util.Browser;
import com.inwk.valoepi.util.PropertyLoader;
import com.inwk.valoepi.webdriver.WebDriverFactory;



public class AppManager {
	  private WebDriver driver;
	  private WebDriverWait wait;
	  private String baseUrl;
	  public PageManager pm;
	  
	  public AppManager() {
		    baseUrl = PropertyLoader.loadProperty("site.url");
		    String gridHubUrl = PropertyLoader.loadProperty("grid2.hub");
		    Browser browser = new Browser();
		    String username = PropertyLoader.loadProperty("user.username");
		    String password = PropertyLoader.loadProperty("user.password");
		    final Thread thread = Thread.currentThread();
		    //System.out.printf("#%d %s: ", thread.getId(), thread.getName());
		    
		    if(thread.getName().endsWith("1")) {
		    	browser.setName("chrome");
		    } else if(thread.getName().endsWith("2"))  {
		    	browser.setName("firefox");
		    } else {
		    	browser.setName("ie");
		    }
		 
		    //browser.setName(PropertyLoader.loadProperty("browser.name"));
		    browser.setVersion(PropertyLoader.loadProperty("browser.version"));
		    browser.setPlatform(PropertyLoader.loadProperty("browser.platform"));
		    
		    driver = WebDriverFactory.getInstance(gridHubUrl, browser, username, password);
		    // driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		    driver.manage().window().maximize();
		    driver.get(baseUrl);
		    pm = new PageManager(this);	
	  }
  
	  public WebDriver getWebDriver() {
		  return driver;
	  }
	  

	  public String getBaseUrl() {
		  return baseUrl;
	  }
	  
	  public PageManager getPageManager() {
		  return pm;
	  }

	  public void stop() {
		  if (driver != null) {
		      driver.quit();
		  }
	  }

}
